package com.bbx.taxi.client.Util;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.comm.returns.line.Cities;
import com.bbx.api.sdk.model.comm.returns.line.To_Cities;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.OfficialOrderList;
import com.bbx.taxi.official.R;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.DB.LineDB;

public class Tel {
	/**
	 * 拨打客服电话
	 */
	public static void onTel_KF(Context context,OfficialOrderList mOfficialOrderList) {
		LineDB db_line = new LineDB(context);
		ToastUtil.showToast(context, R.string.tel_kf);
		String start_name = mOfficialOrderList.getOrderDetail().getStart()
				.getAddress();
		String start_city = "";
		if (start_name.contains(" ")) {
			start_city = start_name.substring(0, start_name.indexOf(" "));
		} else if (start_name.contains("\t")) {
			start_city = start_name.substring(0, start_name.indexOf("\t"));
		} else if (start_name.contains("-")) {
			start_city = start_name.substring(0, start_name.indexOf("-"));
		} else {
			start_city = start_name;
		}
		String end_name = mOfficialOrderList.getOrderDetail().getEnd()
				.getAddress();
		String end_city = "";
		if (end_name.contains(" ")) {
			end_city = end_name.substring(0, end_name.indexOf(" "));
		} else if (end_name.contains("\t")) {
			end_city = end_name.substring(0, end_name.indexOf("\t"));
		} else if (end_name.contains("-")) {
			end_city = end_name.substring(0, end_name.indexOf("-"));
		} else {
			end_city = end_name;
		}
		String tel_kf = "";
		ArrayList<To_Cities> to_citylist = db_line.readToCityDB(start_city,
				Integer.parseInt(mOfficialOrderList.getOrder_type()) == Value.ORDER_TYPE_SN);
		if (to_citylist != null)
			for (int i = 0; i < to_citylist.size(); i++) {
				if (end_city.startsWith(to_citylist.get(i).getCn_Name())) {
					tel_kf = to_citylist.get(i).getService_Phone();
					break;
				}
			}
		// 用intent启动拨打电话
		context.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
				+ tel_kf)));
	}
	/**
	 * 拨打客服电话,传递线路id
	 */
	public static void onTel_KF(Context context,String line_id) {
		LineDB db_line = new LineDB(context);
		List<Cities> citieslist = new ArrayList<Cities>();
		citieslist.addAll(db_line.readAllDB());
		String tel_kf = "";
		if (citieslist != null && !citieslist.isEmpty()) {
			for (int i = 0; i < citieslist.size(); i++) {
				ArrayList<To_Cities> to_citylist = ToCity.getToCity(context, citieslist.get(i).getCn_Name(),
						(citieslist.get(i).getIs_city()!=null&&citieslist.get(i).getIs_city().equals(Value.CITY_INCITY)));
				if (to_citylist != null)
					for (int j = 0; j < to_citylist.size(); j++) {
						if (line_id.equals(to_citylist.get(j).getLine_id())) {
							tel_kf = to_citylist.get(j).getService_Phone();
							break;
						}
					}
			}
		}
		
		// 用intent启动拨打电话
		context.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
				+ tel_kf)));
	}
	public static void onTel(Context context,String tel){
		// 用intent启动拨打电话
		context.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
				+ tel)));
	}
}
